﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Leffaetsin.Controllers;
using Leffaetsin.Models;
using Leffaetsin.Services.Providers.IMDBProviders;
using Leffaetsin.Services.Providers.ShowProviders;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Deserializers;

namespace Leffaetsin.Services
{
    // TODO: areaname ei-case-sensitiivseksi
    public static class MovieService
    {
        public static IsolatedStorageService isolatedStorageService = new IsolatedStorageService(false);

        public static Movie GetMovie(string movieTitle, string areaName)
        {
            // TODO PANU: show:t pitää myös hakea isolatedstorageen
            // TODO PANU: filteröi genren perusteella?
            var movie = ShowProvider.GetMovie(movieTitle, areaName);
            movie.Rating = isolatedStorageService.GetRatingFromStore(movie.OriginalTitle.StripForApi(), movie.ProductionYear);
            //ImdbApiProvider.FillImdbData(movie);

            return movie;
        }


        public static IEnumerable<Movie> GetMovies(string areaName)
        {

            var allMovies = ShowProvider.GetMovies(areaName).ToList();
            //allMovies.ForEach(ImdbApiProvider.FillImdbData);

            allMovies.ForEach(m => m.Rating = isolatedStorageService.GetRatingFromStore(m.OriginalTitle.StripForApi(), m.ProductionYear));
            return allMovies.OrderByDescending(m => m.Rating);
        }

        /*
        private static Dictionary<string, item> MoviesCache = new Dictionary<string, item>();
        private static item GetMovieDataFromImdb(string title, string year)
        {
            item cachedMovie;
            MoviesCache.TryGetValue(title, out cachedMovie);

            if (cachedMovie == null)
            {
                cachedMovie = MoviesCache[title] = GetImdbApiMovie(title, year);
            }
            return cachedMovie;
        }
        */
        
        /*
        public static string GetLargePicture(string title)
        {
            if (String.IsNullOrEmpty(title))
                return "";

            var finnkinoMovie = GetFinnkinoMovies().FirstOrDefault(m => m.Title.StripForApi() == title.StripForApi());
            return (finnkinoMovie == null) ? "" : finnkinoMovie.EventLargeImageLandscape;
        }

        public static string GetThumbnail(string title)
        {
            if (String.IsNullOrEmpty(title))
                return "";

            var finnkinoMovie = GetFinnkinoMovies().FirstOrDefault(m => m.Title.StripForApi() == title.StripForApi());
            return (finnkinoMovie == null) ? "" : finnkinoMovie.EventSmallImagePortrait;
        }*/


        internal static void ForceRefreshMovieData()
        {
            isolatedStorageService = new IsolatedStorageService(true);
        }
    }
}